import { sys } from "cc";
import { Logger } from "./Logger";

export enum GameWebSocketStatus { CONNECTING, OPEN, CLOSING, CLOSED }

/**
 * WebSocket的封装
 */
export class GameWebSocket {

    private socket: WebSocket;

    private onConnect: Function;
    private onClose: Function;
    private onSocketData: Function;
    private onError: Function;
    private thisObject: any;
    public SetCallBacks(onConnect: Function, onClose: Function, onSocketData: Function,
        onError: Function, thisObject: any): void {
        this.onConnect = onConnect;
        this.onClose = onClose;
        this.onSocketData = onSocketData;
        this.onError = onError;
        this.thisObject = thisObject;
    }

    public connect(url: string) {
        if (this.socket) {
            this.socket.close();
        }
        Logger.log("websocket connnect=", url);
        if (sys.isNative) {
            // let cert = cc.url.raw("resources/https/cacert.pem");
            // let cert: string = cc.url.raw("resources/https/https.cert");
            // Logger.log("证书地址===========", cert);
            // if (jsb.fileUtils.isFileExist(cert)) {
            //     // Logger.log("证书===存在========");
            //     this.socket = new WebSocket(url, null, cert);
            // } else {
            // Logger.log("证书==no存在========");
            Logger.log("WebSocket =====", WebSocket);
            Logger.log("WebSocket ===url==", url);
            this.socket = new WebSocket(url);
            // }
        } else {
            this.socket = new WebSocket(url);
        }
        this.socket.binaryType = "arraybuffer";
        this.bindEvent();
    }

    private bindEvent() {
        this.socket.onopen = () => {
            Logger.log("gamewebsocket onopen=");
            this.onConnect.call(this.thisObject);
        }
        this.socket.onclose = () => {
            Logger.log("gamewebsocket close=");
            // GameSocket.getInstance().isNeedReconnect = true
            this.onClose.call(this.thisObject);
            this.clear();
        }
        this.socket.onerror = () => {
            Logger.log("gamewebsocket onerror=");
            this.onError.call(this.thisObject);
        }
        this.socket.onmessage = (event: MessageEvent) => {
            this.onSocketData.call(this.thisObject, event.data);
        }
    }

    private clear() {
        this.unBindEvent();
        this.socket = null;
    }

    /* 
        释放事件，不让他等1分钟后还是回回掉
    */
    private unBindEvent() {
        if (this.socket) {
            this.socket.onopen = null;
            this.socket.onclose = null;
            this.socket.onerror = null;
            this.socket.onmessage = null;
        }
    }

    public close() {
        // Logger.log("gamewebsocket close my", this.socket);
        if (this.socket && this.socket.readyState == GameWebSocketStatus.OPEN) {
            this.socket.close();
        }
        this.clear();
    }

    public send(message: any): void {
        if (this.socket && this.socket.readyState == GameWebSocketStatus.OPEN) {
            // Logger.log("gamewebsocket send", message);
            this.socket.send(message);
        }
    }

    public get readyState() {
        if (this.socket) {
            return this.socket.readyState;
        }
        return GameWebSocketStatus.CLOSED;
    }

}
